﻿using XServer.Models.DrawCards;

namespace XServer.Managers
{
    public class DrawCardsManager
    {
        private readonly Random random = new Random();
        public readonly CardPool _cardPool;
        public readonly string name;

        public DrawCardsManager(CardPool cardPool)
        {
            _cardPool = cardPool;
            name = Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 获取抽卡结果
        /// </summary>
        /// <returns></returns>
        public Card GetCachaResult(int fourCount)
        {
            if (CheckFiveStarGuarantee())
            {
                return GenerateFiveStarCard();
            }

            if (CheckFourStarGuarantee(fourCount))
            {
                return GenerateFourStarCard();
            }

            return GenerateThreeStarCard();
        }

        #region 计算

        /// <summary>
        /// 五星抽卡检查
        /// </summary>
        /// <returns></returns>
        private bool CheckFiveStarGuarantee()
        {
            if (_cardPool.hardPityRemaining == 1)
            {
                // 保底必出
                return true;
            }

            double rate = _cardPool.pityAccumulator < _cardPool.config.fiveStarAddPulls
                ? _cardPool.config.fiveStarProbability
                : _cardPool.config.fiveStarProbability + _cardPool.config.fiveStarAddProbability * (_cardPool.pityAccumulator - _cardPool.config.fiveStarAddPulls);

            var res = random.NextDouble();
            return res < rate;
        }

        /// <summary>
        /// 四星抽卡检查
        /// </summary>
        /// <returns></returns>
        private bool CheckFourStarGuarantee(int fourCount)
        {
            if (_cardPool.fourStarPity == _cardPool.config.fourStarPityPulls - 1)
            {
                return true;
            }

            double rate = _cardPool.fourStarPity < _cardPool.config.fourStarAddPulls
                ? _cardPool.config.fourStarProbability
                : _cardPool.config.fourStarProbability + _cardPool.config.fourStarAddProbability * (_cardPool.fourStarPity - _cardPool.config.fourStarAddPulls);

            if (fourCount > 0)
            {
                rate *= 0.6;
            }

            return random.NextDouble() < rate;
        }

        #endregion

        #region 输出

        /// <summary>
        /// 生成五星
        /// </summary>
        /// <returns></returns>
        private Card GenerateFiveStarCard()
        {
            Card card = new Card()
            {
                cardType = CardType.SSR,
            };

            if (_cardPool.isPityActive == PityState.BigPity)
            {
                // 大保底，必出UP
                _cardPool.GetFiveStar(true);
                card.isUp = true;
            }
            else
            {
                // 动态概率计算
                var res = random.Next(2) == 0;
                _cardPool.GetFiveStar(res);
                card.isUp = res;
            }

            return card;
        }

        /// <summary>
        /// 生成四星
        /// </summary>
        /// <returns></returns>
        private Card GenerateFourStarCard()
        {
            Card card = new Card()
            {
                cardType = CardType.SR,
            };

            var res = random.Next(2) == 0;
            _cardPool.GetFourStar();

            if (res)
            {
                card.name = random.Next(1, 4).ToString();
            }

            return card;
        }

        /// <summary>
        /// 生成三星
        /// </summary>
        /// <returns></returns>
        private Card GenerateThreeStarCard()
        {
            Card card = new Card()
            {
                cardType = CardType.A,
            };

            _cardPool.GetThreeStar();

            return card;
        }

        #endregion
    }
}
